package com.ruoyi.jpbi.controller.konggu;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.jpbi.domain.konggu.Departments;
import com.ruoyi.jpbi.service.DepartmentsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/jpbi/depa")
public class DepartmentController extends BaseController{
        private String prefix = "jpbi/konggu/depa";
        @Autowired
        private DepartmentsService departmentsService;

        /**
         * 跳转分页页面
         */
        @GetMapping()
        public String user() {
            return prefix + "/department";
        }

        @RequestMapping("/getDepaList")
        @ResponseBody
        public TableDataInfo getDepaList(Departments departments) {
            startPage();
            List<Departments> getDepaList = departmentsService.findAllDepa(0+"","all",departments);
            return getDataTable(getDepaList);
        }


    /**
     * 查看公司
     */
    @RequestMapping("/detail")
    public String detail(String id, ModelMap mmap) {
        mmap.put("departments", departmentsService.findDepaone(id));
        return prefix + "/details";
    }




    @RequestMapping("/editDepa")
    public String editDepa(String id, ModelMap mmap) {
        Departments departments = departmentsService.findDepaone(id);
        mmap.put("departments", departments);
        return prefix + "/edit";
    }

    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult edit(Departments departments) {
        return toAjax(departmentsService.updateDepa(departments));
    }


    @GetMapping("/add")
    public String add(Departments departments) {
        return prefix + "/add";
    }

    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Departments departments) {
        return toAjax(departmentsService.insertDepa(departments));
    }

    @RequestMapping("/remove")
    @ResponseBody
    public int remove(String alldepID)
    {
        List<String> depidList = new ArrayList<String>();
        String[] arry = alldepID.split(",");
        for(int i=0;i<arry.length;i++){
            depidList.add(arry[i]);
        }
        return departmentsService.deleteDepa(depidList);
    }




    }

